<?php

$url = "https://sms.tencentcloudapi.com";
$host = "sms.tencentcloudapi.com";//sms.tencentcloudapi.com
$service = "sms";   //sms
$version = "2021-01-11";            //2021-01-11
$action = "SendSms";             //SendSms
$region = "ap-guangzhou";
$timestamp = time();
$algorithm = "TC3-HMAC-SHA256";

$httpRequestMethod = "POST";
$canonicalUri = "/";
$canonicalQueryString = "";
$canonicalHeaders = "content-type:application/json\n"."host:".$host."\nx-tc-action:".strtolower($action)."\n";
$signedHeaders = "content-type;host;x-tc-action";
$secretId="AKIDFVfdQ0RhmhP42qVxFQPE0fDg3ECxVpwt";
$secretKey="wa7VipkV7R1079C6sbny3ypX6WovthMt";

$data=array(
    "PhoneNumberSet" => array("13978860280"),
	"SmsSdkAppId" => "1400926006", //数字也要有又引号
	"TemplateId" => "2222343",
	"SignName" => "熊猫校友会",
	"TemplateParamSet" => array("159456")
);

$jsonstr=json_encode($data, JSON_UNESCAPED_UNICODE);
$hashedRequestPayload = hash("SHA256", $jsonstr);
$canonicalRequest = $httpRequestMethod."\n"
	.$canonicalUri."\n"
	.$canonicalQueryString."\n"
	.$canonicalHeaders."\n"
	.$signedHeaders."\n"
	.$hashedRequestPayload."\n";
echo $canonicalRequest."\n";

$date = gmdate("Y-m-d", $timestamp);
$canonicalRequest=str_replace("\r\n","\n",trim($canonicalRequest));
$credentialScope = $date."/".$service."/tc3_request";
$hashedCanonicalRequest = hash("SHA256", $canonicalRequest);
$stringToSign = $algorithm."\n"
	.$timestamp."\n"
	.$credentialScope."\n"
	.$hashedCanonicalRequest;
echo "\n\n\n".$stringToSign."\r\n\r\n";
$stringToSign=str_replace("\r\n","\n",trim($stringToSign));
$secretDate = hash_hmac("SHA256", $date, "TC3".$secretKey, true);
$secretService = hash_hmac("SHA256", $service, $secretDate, true);
$secretSigning = hash_hmac("SHA256", "tc3_request", $secretService, true);
$signature = hash_hmac("SHA256", $stringToSign, $secretSigning);
$authorization = $algorithm." Credential=".$secretId."/".$credentialScope.", SignedHeaders=content-type;host;x-tc-action, Signature=".$signature;
$authorization=str_replace("\r\n","\n",trim($authorization));
echo "\n\n\n".$authorization."\r\n\r\n";

$headers=array();
$headers[]="Authorization: TC3-HMAC-SHA256 Credential=AKIDFVfdQ0RhmhP42qVxFQPE0fDg3ECxVpwt/2024-12-20/sms/tc3_request, SignedHeaders=content-type;host, Signature=97edd39459649cfdea30ba666d6b12f1f8ef77f07ae065f45d29cbbc0b922ec2";
$headers[]="Content-Type: application/json;";
$headers[]="X-TC-Action: {$action}";
$headers[]="X-TC-Timestamp: {$timestamp}";
$headers[]="X-TC-Version: {$version}";
$headers[]="X-TC-Region: {$region}";
$headers[]="X-TC-Language: zh-CN";
//$headers[]="X-TC-Token: 088qdpS7US3xhpQEo5vU72lKB5B99Pbae079e1b1aad1c19ab190f3788e3bf86e_tZrog99aUH-A0HJmcOInrgEGEJqLHh6G83Ts7wl9Aq-nbWI33-ntvz38O1wb5-eNTbfTuFPOdqO0dJjD8UGAH3KwYp25tCUvcrjA7mfSHVfS5bGQbsC6uLXBBB4oblwRJNZnBfvpJ56aoIALJLc2Q8iwbTmycwPYvQuXmOWw3N7CvbVMwWUysS_Ssg6kaEjoHR8qJd9ZOJL2AmnZT66Fw";


// 初始化一个cURL会话
$ch = curl_init();

// 设置URL和其他选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);      
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonstr);  

$response = curl_exec($ch);

// 检查是否有错误发生
if ($response === false) {
    echo 'Curl error: '. curl_error($ch);
} else {
    echo $response;
}

// 关闭cURL会话
curl_close($ch);